﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Converter;
using csla4Skeleton.UI.SL.Resources.Utility;
using csla4Skeleton.UI.SL.DanhMuc.DM_NhaCungCap;
using Csla.Xaml;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.DM_LoaiHoaDon;
using csla4Skeleton.UI.SL.DanhMuc.DM_Kho;
using csla4Skeleton.UI.SL.DanhMuc.DM_HangHoa;
using csla4Skeleton.UI.SL.DanhMuc.DM_TaiKhoan;
using csla4Skeleton.UI.SL.DanhMuc.DM_DonViTinh;
using csla4Skeleton.UI.SL.DanhMuc.DM_ThueSuat;
using csla4Skeleton.UI.SL.DanhMuc.DM_TinhChatHangHoa;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.DM_LoaiMuaSanPham;
using csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.DM_DoiTuongTHCP;
using Accounting.Library;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System.ComponentModel;
using Utility;
using Accounting.Library.Utility;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.MSP_MuaHangHoa;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.MSP_MuaDichVu;
using ConfigurationNghiepVu;

namespace csla4Skeleton.UI.SL.NghiepVu.TienMat.TM_PhieuChi
{
    public partial class TM_PhieuChi_ChildWindow: ChildWindow
    {
        public String NguoiNop;
        public object ChiTietModel;
        public object PhieuChiModel = null;
        public NghiepVuType type;
        public int CountSave = 0;
        public event EventHandler OnClosedChildWindow;
        public event EventHandler LapPhieuChiCompleteted;
        List<Control> controls = new List<Control>();
        protected TM_PhieuChi_ViewModel TM_PhieuChi_ViewModel;
        private System.Nullable<int> Khoa = null;


         public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            try
            {
                if (parameters == "")
                    Khoa = null;
                else
                    Khoa = Int32.Parse(parameters);
            }
            catch
            {
                Khoa = null;
            }
        }

        
        public TM_PhieuChi_ChildWindow(object PhieuChiModel , object ChiTietModel,NghiepVuType type)
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.NghiepVu.TienMat.TM_PhieuChi.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            InitializeComponent();
            this.type = type;
            this.PhieuChiModel = PhieuChiModel;
            this.ChiTietModel = ChiTietModel;
            if (this.type.Equals(NghiepVuType.MuaHangHoa))
            {
                this.MuaHangHoaTabControl.Visibility = Visibility.Visible;
            }
            else if (this.type.Equals(NghiepVuType.MuaDichVu))
            {
                this.MuaDichVuTabControl.Visibility = Visibility.Visible;
            }
            
        }
        public TM_PhieuChi_ChildWindow(String NguoiNop, object ChiTietModel, NghiepVuType type)
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.NghiepVu.TienMat.TM_PhieuChi.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            InitializeComponent();
            this.NguoiNop = NguoiNop;
            this.type = type;
            this.ChiTietModel = ChiTietModel;
            if(this.type.Equals(NghiepVuType.MuaHangHoa))
            {
                this.MuaHangHoaTabControl.Visibility = Visibility.Visible;
            }
            else if(this.type.Equals(NghiepVuType.MuaDichVu))
            {
                this.MuaDichVuTabControl.Visibility = Visibility.Visible;
            }


        }

         public TM_PhieuChi_ChildWindow()
        {
           
            InitializeComponent();
    
        }
      
         private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                System.Windows.Data.CollectionViewSource TM_PhieuChi_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TM_PhieuChi_ViewModelSource"];
                if( this.PhieuChiModel == null)
                {
                    TM_PhieuChi_ViewModel = new TM_PhieuChi_ViewModel(BusinessConfiguration.Configuration.ChildWindow);                    
                    TM_PhieuChi_ViewModel.Saved += new EventHandler(TM_PhieuChi_ViewModel_Saved);
                    TM_PhieuChi_ViewModel.Canceled += new EventHandler(TM_PhieuChi_ViewModel_Canceled);
                    TM_PhieuChi_ViewModel.LoadCompleted += new EventHandler(TM_PhieuChi_ViewModel_LoadCompleted);
                    
                    TM_PhieuChi_ViewModel.hangHoaIdChanged += new TM_PhieuChi_ViewModel.HangHoaIdChanged_EventHandler(OnHangHoaIdChanged);
                    TM_PhieuChi_CollectionViewSource.Source = new List<object> { TM_PhieuChi_ViewModel };
                }
                else
                {
                    
                    TM_PhieuChi_ViewModel = this.PhieuChiModel as TM_PhieuChi_ViewModel;
                    TM_PhieuChi_CollectionViewSource.Source = new List<object> { TM_PhieuChi_ViewModel };
                    TM_PhieuChi_ViewModel.Saved += new EventHandler(TM_PhieuChi_ViewModel_Saved);
                    TM_PhieuChi_ViewModel.Canceled += new EventHandler(TM_PhieuChi_ViewModel_Canceled);
                   
                    TM_PhieuChi_ViewModel.hangHoaIdChanged += new TM_PhieuChi_ViewModel.HangHoaIdChanged_EventHandler(OnHangHoaIdChanged);

                    if(this.type.Equals(NghiepVuType.MuaHangHoa))
                    {
                        System.Windows.Data.CollectionViewSource msp_MuaHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaHangHoa_ViewModelSource"];
                        MSP_MuaHangHoa_ViewModel muaHangHoaViewModel = this.ChiTietModel as MSP_MuaHangHoa_ViewModel;
                        muaHangHoaViewModel.ChildList_AddedNew += new MSP_MuaHangHoa_ViewModel.ChildList_AddedNew_EventHandler(msp_MuaHangHoa_ViewModel_ChildList_AddedNew);
                        muaHangHoaViewModel.hangHoaIdChanged += new MSP_MuaHangHoa_ViewModel.HangHoaIdChanged_EventHandler(msp_MuaHangHoa_HangHoaIdChanged);
                        muaHangHoaViewModel.ThueSuatIdChanged += new MSP_MuaHangHoa_ViewModel.ThueSuatIdChanged_EventHandler(msp_MuaHangHoa_ThueSuatIdChanged);
                        muaHangHoaViewModel.TongTienHangChanged += new EventHandler(OnTongTienHangChanged);
                        muaHangHoaViewModel.TienThueGTGTChanged += new EventHandler(OnTienThueGTGTChanged);
                        muaHangHoaViewModel.TongTienThanhToanChanged += new EventHandler(OnTongTienThanhToanChanged);
                        msp_MuaHangHoa_CollectionViewSource.Source = new List<object> { muaHangHoaViewModel };
                        DoLoadForMuaHangHoa();
  
                    } 
                    else if(this.type.Equals(NghiepVuType.MuaDichVu))
                    {
                        System.Windows.Data.CollectionViewSource msp_MuaDichVu_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaDichVu_ViewModelSource"];
                        MSP_MuaDichVu_ViewModel muaDichVuViewModel = this.ChiTietModel as MSP_MuaDichVu_ViewModel;
                        muaDichVuViewModel.ChildList_AddedNew += new MSP_MuaDichVu_ViewModel.ChildList_AddedNew_EventHandler(MSP_MuaDichVu_ViewModel_ChildList_AddedNew);
                        muaDichVuViewModel.hangHoaIdChanged += new MSP_MuaDichVu_ViewModel.HangHoaIdChanged_EventHandler(msp_MuaDichVu_HangHoaIdChanged);
                        muaDichVuViewModel.ThueSuatIdChanged += new MSP_MuaDichVu_ViewModel.ThueSuatIdChanged_EventHandler(msp_MuaDichVu_ThueSuatIdChanged);
                        muaDichVuViewModel.TongTienHangChanged += new EventHandler(OnTongTienHangChanged);
                        muaDichVuViewModel.TienThueGTGTChanged += new EventHandler(OnTienThueGTGTChanged);
                        muaDichVuViewModel.TongTienThanhToanChanged += new EventHandler(OnTongTienThanhToanChanged);
                        msp_MuaDichVu_CollectionViewSource.Source = new List<object> { muaDichVuViewModel };
                        DoLoadForMuaDichVu();
                    } 

                }

                System.Windows.Data.CollectionViewSource dm_NhaCungCap_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
                DM_NhaCungCap_ListViewModel dm_NhaCungCap_ListViewModel = new DM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_NhaCungCap);
                dm_NhaCungCap_CollectionViewSource.Source = new List<object> { dm_NhaCungCap_ListViewModel };


                System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
                DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = new DM_HangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_HangHoa_CollectionViewSource.Source = new List<object> { dm_HangHoa_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_LoaiHoaDon_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiHoaDon_ListViewModelSource"];
                DM_LoaiHoaDon_ListViewModel dm_LoaiHoaDon_ListViewModel = new DM_LoaiHoaDon_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_LoaiHoaDon_CollectionViewSource.Source = new List<object> { dm_LoaiHoaDon_ListViewModel };


                System.Windows.Data.CollectionViewSource dm_DonViTinh_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_DonViTinh_ListViewModelSource"];
                DM_DonViTinh_ListViewModel dm_DonViTinh_ListViewModel = new DM_DonViTinh_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_DonViTinh_CollectionViewSource.Source = new List<object> { dm_DonViTinh_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_ChiTiet_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoanNo_ChiTiet_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoanNo_ChiTiet_CollectionViewSource.Source = new List<object> { dm_TaiKhoanNo_ChiTiet_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_ChiTiet_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoanCo_ChiTiet_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoanCo_ChiTiet_CollectionViewSource.Source = new List<object> { dm_TaiKhoanCo_ChiTiet_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_Thue_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_Thue_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoanNo_Thue_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoanNo_Thue_CollectionViewSource.Source = new List<object> { dm_TaiKhoanNo_Thue_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_Thue_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_Thue_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoanCo_Thue_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoanCo_Thue_CollectionViewSource.Source = new List<object> { dm_TaiKhoanCo_Thue_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_DoiTuongTHCP_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_DoiTuongTHCP_ListViewModelSource"];
                DM_DoiTuongTHCP_ListViewModel dm_DoiTuongTHCP_ListViewModel = new DM_DoiTuongTHCP_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_DoiTuongTHCP_CollectionViewSource.Source = new List<object> { dm_DoiTuongTHCP_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoan_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoan_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoan_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoan_CollectionViewSource.Source = new List<object> { dm_TaiKhoan_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_Kho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_Kho_ListViewModelSource"];
                DM_Kho_ListViewModel dm_Kho_ListViewModel = new DM_Kho_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_Kho_CollectionViewSource.Source = new List<object> { dm_Kho_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_LoaiMuaSanPham_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiMuaSanPham_ListViewModelSource"];
                DM_LoaiMuaSanPham_ListViewModel dm_LoaiMuaSanPham_ListViewModel = new DM_LoaiMuaSanPham_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_LoaiMuaSanPham_CollectionViewSource.Source = new List<object> { dm_LoaiMuaSanPham_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_ThueSuat_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_ThueSuat_ListViewModelSource"]; 
                DM_ThueSuat_ListViewModel dm_ThueSuat_ListViewModel = new DM_ThueSuat_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_ThueSuat_CollectionViewSource.Source = new List<object> { dm_ThueSuat_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TinhChatHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TinhChatHangHoa_ListViewModelSource"];
                DM_TinhChatHangHoa_ListViewModel dm_TinhChatHangHoa_ListViewModel = new DM_TinhChatHangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TinhChatHangHoa_CollectionViewSource.Source = new List<object> { dm_TinhChatHangHoa_ListViewModel };
                
               
                
            }
        }

         private void OnTongTienThanhToanChanged(object sender, EventArgs e)
         {
             if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
             {
                 this.GetViewModel().Model.TongTienThanhToan = this.GetMuaHangHoaViewModel().Model.TongTienThanhToan;
                 this.GetViewModel().Model.TongTienThanhToanQĐ = this.GetMuaHangHoaViewModel().Model.TongTienThanhToan * GetMuaHangHoaViewModel().Model.TyGia;
             }
             else if (this.MuaDichVuTabControl.Visibility == Visibility.Visible)
             {
                 this.GetViewModel().Model.TongTienThanhToan = this.GetMuaDichVuViewModel().Model.TongTienThanhToan;
                 this.GetViewModel().Model.TongTienThanhToanQĐ = this.GetMuaDichVuViewModel().Model.TongTienThanhToan * GetMuaDichVuViewModel().Model.TyGia;
             }
         }

         private void OnTongTienHangChanged(object sender, EventArgs e)
         {
             if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
             {
                 this.GetViewModel().Model.TongTien = this.GetMuaHangHoaViewModel().Model.TongTienHang;
                 this.GetViewModel().Model.TongTienQĐ = this.GetMuaHangHoaViewModel().Model.TongTienHang * GetMuaHangHoaViewModel().Model.TyGia;
             }
             else if (this.MuaDichVuTabControl.Visibility == Visibility.Visible)
             {
                 this.GetViewModel().Model.TongTien = this.GetMuaDichVuViewModel().Model.TongTienHang;
                 this.GetViewModel().Model.TongTienQĐ = this.GetMuaDichVuViewModel().Model.TongTienHang * GetMuaDichVuViewModel().Model.TyGia;
             }
         }

         private void OnTienThueGTGTChanged(object sender, EventArgs e)
         {
             if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
             {
                 this.GetViewModel().Model.TienThueGTGT = this.GetMuaHangHoaViewModel().Model.TienThueGTGT;
                 this.GetViewModel().Model.TienThueGTGTQĐ = this.GetMuaHangHoaViewModel().Model.TienThueGTGT * GetMuaHangHoaViewModel().Model.TyGia;
             }
             else if (this.MuaDichVuTabControl.Visibility == Visibility.Visible)
             {
                 this.GetViewModel().Model.TienThueGTGT = this.GetMuaDichVuViewModel().Model.TienThueGTGT;
                 this.GetViewModel().Model.TienThueGTGTQĐ = this.GetMuaDichVuViewModel().Model.TienThueGTGT * GetMuaDichVuViewModel().Model.TyGia;
             }
         }

        private void OnHangHoaIdChanged(object sender, HangHoaIdChangedEventArgs e)
        {
          
        }
        private TM_PhieuChi_ViewModel GetViewModel()
        {
            System.Windows.Data.CollectionViewSource TM_PhieuChi_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TM_PhieuChi_ViewModelSource"];
            return ((List<object>)TM_PhieuChi_CollectionViewSource.Source)[0] as TM_PhieuChi_ViewModel;
        }

        protected override void OnClosed(EventArgs e)
        {
            if (OnClosedChildWindow != null)
            {
                OnClosedChildWindow(this, new EventArgs());
            }
            base.OnClosed(e);

        }
        private void DoLoadForMuaHangHoa()
        {
            System.Windows.Data.CollectionViewSource msp_MuaHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaHangHoa_ViewModelSource"];
            MSP_MuaHangHoa_ViewModel muaHangHoaViewModel = ((List<object>)msp_MuaHangHoa_CollectionViewSource.Source)[0] as MSP_MuaHangHoa_ViewModel;

            this.GetViewModel().Model.TongTienThanhToan = muaHangHoaViewModel.Model.TongTienThanhToan;
            this.GetViewModel().Model.TongTienThanhToanQĐ = muaHangHoaViewModel.Model.TongTienThanhToanQĐ;

            this.GetViewModel().Model.TongTien = muaHangHoaViewModel.Model.TongTienHang;
            this.GetViewModel().Model.TongTienQĐ = muaHangHoaViewModel.Model.TongTienHangQĐ;

            this.GetViewModel().Model.TienThueGTGT = muaHangHoaViewModel.Model.TienThueGTGT;
            this.GetViewModel().Model.TienThueGTGTQĐ = muaHangHoaViewModel.Model.TienThueGTGTQĐ;

            if (muaHangHoaViewModel.Model.LoaiMuaSanPhamId == 1)
            {
                this.MSP_MuaHangHoa_ThueNKTab.Visibility = Visibility.Collapsed;
                this.MSP_MuaHangHoa_ThueTTDBTab.Visibility = Visibility.Collapsed;
            }
            if (muaHangHoaViewModel.Model.LoaiTienId == 3)
            {
                this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.Columns[8].IsVisible = false;
                this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.Columns[10].IsVisible = false;
                this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.Columns[13].IsVisible = false;
                this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.Columns[15].IsVisible = false;
                this.MSP_MuaHangHoa_ThueNKGridView.Columns[4].IsVisible = false;
                this.MSP_MuaHangHoa_ThueTTDBGridView.Columns[4].IsVisible = false;
                this.MSP_MuaHangHoa_ThueChiTietGridView.Columns[4].IsVisible = false;
                this.MSP_MuaHangHoa_TSThieuCXLGridView.Columns[3].IsVisible = false;
                this.MSP_MuaHangHoa_TSThuaCXLGridView.Columns[3].IsVisible = false;
                this.MSP_MuaHangHoa_ChiPhiMuaHangGridView.Columns[3].IsVisible = false;
            }
            this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView);
            this.MSP_MuaHangHoa_ThueChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.MSP_MuaHangHoa_ThueChiTietGridView);
            this.MSP_MuaHangHoa_ChiPhiMuaHangGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.MSP_MuaHangHoa_ChiPhiMuaHangGridView);
            this.MSP_MuaHangHoa_TSThieuCXLGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.MSP_MuaHangHoa_TSThieuCXLGridView);
            this.MSP_MuaHangHoa_TSThuaCXLGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.MSP_MuaHangHoa_TSThuaCXLGridView);
            this.MSP_MuaHangHoa_ThueNKGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.MSP_MuaHangHoa_ThueNKGridView);
            this.MSP_MuaHangHoa_ThueTTDBGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.MSP_MuaHangHoa_ThueTTDBGridView);

            ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
            ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_ThueChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
            ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_ChiPhiMuaHangGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
            ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_TSThieuCXLGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
            ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_TSThuaCXLGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
            ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_ThueNKGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
            ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_ThueTTDBGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);

            ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
            ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_ThueChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
            ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_ChiPhiMuaHangGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
            ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_TSThieuCXLGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
            ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_TSThuaCXLGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
            ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_ThueNKGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
            ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaHangHoa_ThueTTDBGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);

            if (this.GetMuaHangHoaViewModel().CanSave == true)
            {
                GetMuaHangHoaViewModel().Save(this, new Csla.Xaml.ExecuteEventArgs());
            }
        }
        private void DoLoadForMuaDichVu()
        {
            System.Windows.Data.CollectionViewSource msp_MuaDichVu_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaDichVu_ViewModelSource"];
            MSP_MuaDichVu_ViewModel muaDichVuViewModel = ((List<object>)msp_MuaDichVu_CollectionViewSource.Source)[0] as MSP_MuaDichVu_ViewModel;

            this.GetViewModel().Model.TongTienThanhToan = muaDichVuViewModel.Model.TongTienThanhToan;
            this.GetViewModel().Model.TongTienThanhToanQĐ = muaDichVuViewModel.Model.TongTienThanhToanQĐ;

            this.GetViewModel().Model.TongTien = muaDichVuViewModel.Model.TongTienHang;
            this.GetViewModel().Model.TongTienQĐ = muaDichVuViewModel.Model.TongTienHangQĐ;

            this.GetViewModel().Model.TienThueGTGT = muaDichVuViewModel.Model.TienThueGTGT;
            this.GetViewModel().Model.TienThueGTGTQĐ = muaDichVuViewModel.Model.TienThueGTGTQĐ;

            if (muaDichVuViewModel.Model.LoaiTienId == 3)
            {
                this.MSP_MuaDichVu_MuaDichVuChiTietGridView.Columns[6].IsVisible = false;
                this.MSP_MuaDichVu_MuaDichVuChiTietGridView.Columns[8].IsVisible = false;
                this.MSP_MuaDichVu_MuaDichVuChiTietGridView.Columns[11].IsVisible = false;
                this.MSP_MuaDichVu_ThueChiTietGridView.Columns[3].IsVisible = false;
                this.MSP_MuaDichVu_ChiPhiMuaDichVuGridView.Columns[2].IsVisible = false;

            }
            this.MSP_MuaDichVu_MuaDichVuChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.MSP_MuaDichVu_MuaDichVuChiTietGridView);
            this.MSP_MuaDichVu_ThueChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.MSP_MuaDichVu_ThueChiTietGridView);
            this.MSP_MuaDichVu_ChiPhiMuaDichVuGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.MSP_MuaDichVu_ChiPhiMuaDichVuGridView);

            ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaDichVu_MuaDichVuChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
            ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaDichVu_ThueChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
            ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaDichVu_ChiPhiMuaDichVuGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);

            ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaDichVu_MuaDichVuChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
            ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaDichVu_ThueChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
            ((ControlBehavior.GridViewKeyboardCommandProvider)this.MSP_MuaDichVu_ChiPhiMuaDichVuGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
        }
        void TM_PhieuChi_ViewModel_Saved(object sender, EventArgs e)
        {
            if (LapPhieuChiCompleteted != null)
            {
                LapPhieuChiCompleteted(this, new EventArgs());
            }     
        }
        void TM_PhieuChi_ViewModel_Canceled(object sender, EventArgs e)
        {
            this.DialogResult = false;
        }

       
        void TM_PhieuChi_ViewModel_LoadCompleted(object sender, EventArgs e)
        {
            if(this.type == NghiepVuType.MuaHangHoa)
            {
               
                System.Windows.Data.CollectionViewSource msp_MuaHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaHangHoa_ViewModelSource"];
               
                MSP_MuaHangHoa_ViewModel muaHangHoaViewModel = this.ChiTietModel as MSP_MuaHangHoa_ViewModel;
                muaHangHoaViewModel.ChildList_AddedNew += new MSP_MuaHangHoa_ViewModel.ChildList_AddedNew_EventHandler(msp_MuaHangHoa_ViewModel_ChildList_AddedNew);
                muaHangHoaViewModel.hangHoaIdChanged += new MSP_MuaHangHoa_ViewModel.HangHoaIdChanged_EventHandler(msp_MuaHangHoa_HangHoaIdChanged);
                muaHangHoaViewModel.ThueSuatIdChanged += new MSP_MuaHangHoa_ViewModel.ThueSuatIdChanged_EventHandler(msp_MuaHangHoa_ThueSuatIdChanged);
                muaHangHoaViewModel.TongTienThanhToanChanged += new EventHandler(OnTongTienThanhToanChanged);
                muaHangHoaViewModel.TongTienHangChanged += new EventHandler(OnTongTienHangChanged);
                muaHangHoaViewModel.TienThueGTGTChanged += new EventHandler(OnTienThueGTGTChanged);
               
                msp_MuaHangHoa_CollectionViewSource.Source = new List<object> { muaHangHoaViewModel };

                TM_PhieuChi_ViewModel phieuChiViewModel = this.GetViewModel();
                phieuChiViewModel.Model.NhaCungCapId = muaHangHoaViewModel.Model.NhaCungCapId;
                phieuChiViewModel.Model.NguoiNop = this.NguoiNop;
                phieuChiViewModel.Model.DienGiai = "Chi tiền mua hàng hóa";
                phieuChiViewModel.Model.LoaiChungTuThayTheId = muaHangHoaViewModel.Model.LoaiChungTuId;
                phieuChiViewModel.Model.ChungTuThayTheId = muaHangHoaViewModel.Model.Khoa;
               
                phieuChiViewModel.Model.TongTien = muaHangHoaViewModel.Model.TongTienHang;
                phieuChiViewModel.Model.TongTienQĐ = muaHangHoaViewModel.Model.TongTienHangQĐ;

                phieuChiViewModel.Model.TienThueGTGT = muaHangHoaViewModel.Model.TienThueGTGT;
                phieuChiViewModel.Model.TienThueGTGTQĐ = muaHangHoaViewModel.Model.TienThueGTGTQĐ;

                phieuChiViewModel.Model.TongTienThanhToan = muaHangHoaViewModel.Model.TongTienThanhToan;
                phieuChiViewModel.Model.TongTienThanhToanQĐ = muaHangHoaViewModel.Model.TongTienThanhToanQĐ;

                if (muaHangHoaViewModel.Model.LoaiMuaSanPhamId == 1)
                {
                    this.MSP_MuaHangHoa_ThueNKTab.Visibility = Visibility.Collapsed;
                    this.MSP_MuaHangHoa_ThueTTDBTab.Visibility = Visibility.Collapsed;
                }
                if (muaHangHoaViewModel.Model.LoaiTienId == 3)
                {
                    this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.Columns[8].IsVisible = false;
                    this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.Columns[10].IsVisible = false;
                    this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.Columns[13].IsVisible = false;
                    this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.Columns[15].IsVisible = false;
                    this.MSP_MuaHangHoa_ThueNKGridView.Columns[4].IsVisible = false;
                    this.MSP_MuaHangHoa_ThueTTDBGridView.Columns[4].IsVisible = false;
                    this.MSP_MuaHangHoa_ThueChiTietGridView.Columns[4].IsVisible = false;
                    this.MSP_MuaHangHoa_TSThieuCXLGridView.Columns[3].IsVisible = false;
                    this.MSP_MuaHangHoa_TSThuaCXLGridView.Columns[3].IsVisible = false;
                    this.MSP_MuaHangHoa_ChiPhiMuaHangGridView.Columns[3].IsVisible = false;
                }
                phieuChiViewModel.Save(this, new Csla.Xaml.ExecuteEventArgs());

                
               
            }
            else if (this.type == NghiepVuType.MuaDichVu)
            {

                System.Windows.Data.CollectionViewSource msp_MuaDichVu_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaDichVu_ViewModelSource"];
                MSP_MuaDichVu_ViewModel muaDichVuViewModel = this.ChiTietModel as MSP_MuaDichVu_ViewModel;
                muaDichVuViewModel.TongTienThanhToanChanged += new EventHandler(OnTongTienThanhToanChanged);
                muaDichVuViewModel.TongTienHangChanged += new EventHandler(OnTongTienHangChanged);
                muaDichVuViewModel.TienThueGTGTChanged += new EventHandler(OnTienThueGTGTChanged);
                msp_MuaDichVu_CollectionViewSource.Source = new List<object> { muaDichVuViewModel };


               
                TM_PhieuChi_ViewModel phieuChiViewModel = this.GetViewModel();
                phieuChiViewModel.Model.NhaCungCapId = muaDichVuViewModel.Model.NhaCungCapId;
                phieuChiViewModel.Model.NguoiNop = this.NguoiNop;
                phieuChiViewModel.Model.DienGiai = "Chi tiền mua dịch vụ";
                phieuChiViewModel.Model.LoaiChungTuThayTheId = muaDichVuViewModel.Model.LoaiChungTuId;
                phieuChiViewModel.Model.ChungTuThayTheId = muaDichVuViewModel.Model.Khoa;
                
                phieuChiViewModel.Model.TongTien = muaDichVuViewModel.Model.TongTienHang;
                phieuChiViewModel.Model.TongTienQĐ = muaDichVuViewModel.Model.TongTienHangQĐ;

                phieuChiViewModel.Model.TienThueGTGT = muaDichVuViewModel.Model.TienThueGTGT;
                phieuChiViewModel.Model.TienThueGTGTQĐ = muaDichVuViewModel.Model.TienThueGTGTQĐ;

                phieuChiViewModel.Model.TongTienThanhToan = muaDichVuViewModel.Model.TongTienThanhToan;
                phieuChiViewModel.Model.TongTienThanhToanQĐ = muaDichVuViewModel.Model.TongTienThanhToanQĐ;

                if (muaDichVuViewModel.Model.LoaiTienId == 3)
                {
                    this.MSP_MuaDichVu_MuaDichVuChiTietGridView.Columns[6].IsVisible = false;
                    this.MSP_MuaDichVu_MuaDichVuChiTietGridView.Columns[8].IsVisible = false;
                    this.MSP_MuaDichVu_MuaDichVuChiTietGridView.Columns[11].IsVisible = false;
                    this.MSP_MuaDichVu_ThueChiTietGridView.Columns[3].IsVisible = false;
                    this.MSP_MuaDichVu_ChiPhiMuaDichVuGridView.Columns[2].IsVisible = false;
                   
                }
                phieuChiViewModel.Save(this, new Csla.Xaml.ExecuteEventArgs());

               
            }
        }


        private void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                TM_PhieuChi_ViewModel vm = GetViewModel();
                if (e.Key == Key.S)
                {
                    if (vm.CanSave)
                    {
                        vm.Save(sender, new Csla.Xaml.ExecuteEventArgs());
                    }
                }
                else if (e.Key == Key.F6)
                {
                    AddItems();
                }
                

            }
            e.Handled = true;
        }

        void AddItemButton_Click(object sender, RoutedEventArgs e)
        {

            AddItems();
        }

        void DeleteItemButton_Click(object sender, RoutedEventArgs e)
        {
            DeleteItems();

        }

        public void AddItems()
        {
            if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
            {
                if (this.MSP_MuaHangHoa_MuaHangHoaChiTietTab.IsSelected)
                {
                    AddMuaHangHoaDetailsItem();
                }
            }
            else if (this.MuaDichVuTabControl.Visibility == Visibility.Visible)
            {
                if (this.MSP_MuaDichVu_MuaDichVuChiTietTab.IsSelected)
                {
                    AddMuaDichVuDetailsItem();
                }
            }

        }
        private RadGridView GetActiveGridView()
        {
            if (this.MSP_MuaHangHoa_MuaHangHoaChiTietTab.IsSelected)
            {
                return MSP_MuaHangHoa_MuaHangHoaChiTietGridView;
            }
            else if (this.MSP_MuaHangHoa_ThueChiTietTab.IsSelected)
            {
                return MSP_MuaHangHoa_ThueChiTietGridView;
            }
            else if (this.MSP_MuaHangHoa_ChiPhiMuaHangTab.IsSelected)
            {
                return MSP_MuaHangHoa_ChiPhiMuaHangGridView;
            }
            else if (this.MSP_MuaHangHoa_TSThieuCXLTab.IsSelected)
            {
                return MSP_MuaHangHoa_TSThieuCXLGridView;
            }
            else if (this.MSP_MuaHangHoa_TSThuaCXLTab.IsSelected)
            {
                return MSP_MuaHangHoa_TSThuaCXLGridView;
            }
            else if (this.MSP_MuaHangHoa_ThueNKTab.IsSelected)
            {
                return MSP_MuaHangHoa_ThueNKGridView;
            }
            else if (this.MSP_MuaHangHoa_ThueTTDBTab.IsSelected)
            {
                return MSP_MuaHangHoa_ThueTTDBGridView;
            }
            else if (this.MSP_MuaDichVu_MuaDichVuChiTietTab.IsSelected)
            {
                return MSP_MuaDichVu_MuaDichVuChiTietGridView;
            }
            else if (this.MSP_MuaDichVu_ThueChiTietTab.IsSelected)
            {
                return MSP_MuaDichVu_ThueChiTietGridView;
            }
            else if (this.MSP_MuaDichVu_ChiPhiMuaDichVuTab.IsSelected)
            {
                return MSP_MuaDichVu_ChiPhiMuaDichVuGridView;
            }


            return null;
        }



        void DeleteItems()
        {
            if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
            {
                if (this.MSP_MuaHangHoa_MuaHangHoaChiTietTab.IsSelected)
                {
                    MSP_MuaHangHoa_ChiTietChild ChiTietChild = (MSP_MuaHangHoa_ChiTietChild)(this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.SelectedItem);
                    IList<MSP_MuaHangHoa_ChiTietChild> list = this.GetMuaHangHoaViewModel().Model.MSP_MuaHangHoa_ChiTiets;
                    int index = list.IndexOf(ChiTietChild);
                    DeleteMuaHangHoaDetailsItem(index);
                }
            }
            else if (this.MuaDichVuTabControl.Visibility == Visibility.Visible)
            {
                if (this.MSP_MuaDichVu_MuaDichVuChiTietTab.IsSelected)
                {
                    MSP_MuaDichVu_ChiTietChild ChiTietChild = (MSP_MuaDichVu_ChiTietChild)(this.MSP_MuaDichVu_MuaDichVuChiTietGridView.SelectedItem);
                    IList<MSP_MuaDichVu_ChiTietChild> list = this.GetMuaDichVuViewModel().Model.MSP_MuaDichVu_ChiTiets;
                    int index = list.IndexOf(ChiTietChild);
                    DeleteMuaDichVuDetailsItem(index);
                }
            }


        }

        void GridView_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
        {
            var grid = e.OwnerGridViewItemsControl;
            if (grid.Items.Count > 0)
            {
                GridViewUtil.FocusRow((RadGridView)grid, grid.Items.Count - 1);
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[grid.Items.Count - 1], grid.Columns[0]);
                    grid.Focus();
                    grid.BeginEdit();
                }));
            }
        }

        private void SaveAndNewButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
            {
                this.GetMuaHangHoaViewModel().SaveAndNew();
            }
            else if (this.MuaDichVuTabControl.Visibility == Visibility.Visible)
            {
                this.GetMuaDichVuViewModel().SaveAndNew();
            }
        }

        private void DatePicker_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
                    {

                        this.AddMuaHangHoaDetailsItem();
                    }
                    else if (this.MuaDichVuTabControl.Visibility == Visibility.Visible)
                    {
                        this.AddMuaDichVuDetailsItem();
                    }
                }
            }
            else
            {
                ControlBehavior.DatePickerBehaviors.DatePicker_KeyUp(sender, e, controls);
            }
        }

        private void ComboBox_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
                    {

                        this.AddMuaHangHoaDetailsItem();
                    }
                    else if (this.MuaDichVuTabControl.Visibility == Visibility.Visible)
                    {
                        this.AddMuaDichVuDetailsItem();
                    }
                }
            }
            else
            {
                ControlBehavior.ComboBoxBehaviors.ComboBox_KeyUp(sender, e, controls);
            }
        }

        private void Control_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
                    {

                        this.AddMuaHangHoaDetailsItem();
                    }
                    else if (this.MuaDichVuTabControl.Visibility == Visibility.Visible)
                    {
                        this.AddMuaDichVuDetailsItem();
                    }

                }
            }
            else
            {
                ControlBehavior.ControlBehaviors.Control_KeyUp(sender, e, controls);
            }
        }


        private void BookedItemButton_Click(object sender, RoutedEventArgs e)
        {
            GetViewModel().GhiSo();
        }

        private void DeleteBookedItemButton_Click(object sender, RoutedEventArgs e)
        {
            GetViewModel().BoGhi();
        }

        void OnTabKeyPress(object sender, EventArgs e)
        {
            if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
            {
                this.MuaHangHoaTabControl.SelectedItem = MuaHangHoaTabControl.Items[0];
                this.AddMuaHangHoaDetailsItem();
            }
            else if (this.MuaDichVuTabControl.Visibility == Visibility.Visible)
            {
                this.MuaDichVuTabControl.SelectedItem = MuaDichVuTabControl.Items[0];
                this.AddMuaDichVuDetailsItem();
            }

        }

        void OnReachLastColumn(object sender, ReachLastColumnArgs e)
        {
            if (MuaHangHoaTabControl.Visibility == Visibility.Visible)
            {
                List<TabItem> AvailableTabItem = new List<TabItem>();
                int indexCurrentTab = 0;
                foreach (TabItem item in this.MuaHangHoaTabControl.Items)
                {
                    if (item.Visibility == System.Windows.Visibility.Visible)
                    {
                        AvailableTabItem.Add(item);
                        if (item.GetHashCode() == MuaHangHoaTabControl.SelectedItem.GetHashCode())
                        {
                            indexCurrentTab = AvailableTabItem.IndexOf(item);
                        }

                    }
                }


                TabItem[] list = AvailableTabItem.ToArray();
                if (indexCurrentTab == (AvailableTabItem.Count - 1))
                {
                    this.MuaHangHoaTabControl.SelectedItem = list[0];
                    FocusAfterChangeTab(this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView, e.Index);

                }
                else
                {
                    this.MuaHangHoaTabControl.SelectedItem = list[indexCurrentTab + 1];
                    if (list[indexCurrentTab + 1].Name.Equals("MSP_MuaHangHoa_ChiPhiMuaHangTab"))
                    {
                        FocusAfterChangeTab(this.MSP_MuaHangHoa_ChiPhiMuaHangGridView, e.Index);
                    }
                    else if (list[indexCurrentTab + 1].Name.Equals("MSP_MuaHangHoa_ThueChiTietTab"))
                    {
                        FocusAfterChangeTab(this.MSP_MuaHangHoa_ThueChiTietGridView, e.Index);

                    }
                    else if (list[indexCurrentTab + 1].Name.Equals("MSP_MuaHangHoa_ThueNKTab"))
                    {
                        FocusAfterChangeTab(this.MSP_MuaHangHoa_ThueNKGridView, e.Index);

                    }
                    else if (list[indexCurrentTab + 1].Name.Equals("MSP_MuaHangHoa_ThueTTDBTab"))
                    {
                        FocusAfterChangeTab(this.MSP_MuaHangHoa_ThueTTDBGridView, e.Index);

                    }
                    else if (list[indexCurrentTab + 1].Name.Equals("MSP_MuaHangHoa_TSThieuCXLTab"))
                    {
                        FocusAfterChangeTab(this.MSP_MuaHangHoa_TSThieuCXLGridView, e.Index);

                    }
                    else if (list[indexCurrentTab + 1].Name.Equals("MSP_MuaHangHoa_TSThuaCXLTab"))
                    {
                        FocusAfterChangeTab(this.MSP_MuaHangHoa_TSThuaCXLGridView, e.Index);

                    }
                }
            }
            else if (MuaDichVuTabControl.Visibility == Visibility.Visible)
            {
                List<TabItem> AvailableTabItem = new List<TabItem>();
                int indexCurrentTab = 0;
                foreach (TabItem item in this.MuaDichVuTabControl.Items)
                {
                    if (item.Visibility == System.Windows.Visibility.Visible)
                    {
                        AvailableTabItem.Add(item);
                        if (item.GetHashCode() == MuaDichVuTabControl.SelectedItem.GetHashCode())
                        {
                            indexCurrentTab = AvailableTabItem.IndexOf(item);
                        }

                    }
                }


                TabItem[] list = AvailableTabItem.ToArray();
                if (indexCurrentTab == (AvailableTabItem.Count - 1))
                {
                    this.MuaDichVuTabControl.SelectedItem = list[0];
                    FocusAfterChangeTab(this.MSP_MuaDichVu_MuaDichVuChiTietGridView, e.Index);
                }
                else
                {
                    this.MuaDichVuTabControl.SelectedItem = list[indexCurrentTab + 1];
                    if (list[indexCurrentTab + 1].Name.Equals("MSP_MuaDichVu_ChiPhiMuaDichVuTab"))
                    {
                        GridViewUtil.FocusRow(this.MSP_MuaDichVu_ChiPhiMuaDichVuGridView, e.Index);
                        FocusAfterChangeTab(this.MSP_MuaDichVu_ChiPhiMuaDichVuGridView, e.Index);
                    }
                    else if (list[indexCurrentTab + 1].Name.Equals("MSP_MuaDichVu_ThueChiTietTab"))
                    {
                        FocusAfterChangeTab(this.MSP_MuaDichVu_ThueChiTietGridView, e.Index);

                    }


                }
            }

        }

        private void FocusAfterChangeTab(GridViewDataControl grid, int index)
        {

            Dispatcher.BeginInvoke(new Action(() =>
            {
                grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[index], grid.Columns[0]);
                grid.Focus();
                grid.BeginEdit();
            }));

        }
        private MSP_MuaHangHoa_ViewModel GetMuaHangHoaViewModel()
        {
            System.Windows.Data.CollectionViewSource MSP_MuaHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaHangHoa_ViewModelSource"];
            return ((List<object>)MSP_MuaHangHoa_CollectionViewSource.Source)[0] as MSP_MuaHangHoa_ViewModel;
        }

        private MSP_MuaDichVu_ViewModel GetMuaDichVuViewModel()
        {
            System.Windows.Data.CollectionViewSource MSP_MuaDichVu_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaDichVu_ViewModelSource"];
            return ((List<object>)MSP_MuaDichVu_CollectionViewSource.Source)[0] as MSP_MuaDichVu_ViewModel;
        }

        void msp_MuaHangHoa_ThueSuatIdChanged(object sender, ThueSuatIdChangedEventArgs e)
        {

            System.Windows.Data.CollectionViewSource msp_MuaHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaHangHoa_ViewModelSource"];
            MSP_MuaHangHoa_ViewModel muaHangHoaViewModel = ((List<object>)msp_MuaHangHoa_CollectionViewSource.Source)[0] as MSP_MuaHangHoa_ViewModel;

            System.Windows.Data.CollectionViewSource dm_ThueSuat_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_ThueSuat_ListViewModelSource"];
            DM_ThueSuat_ListViewModel dm_ThueSuat_ListViewModel = ((List<object>)dm_ThueSuat_CollectionViewSource.Source)[0] as DM_ThueSuat_ListViewModel;

            MSP_MuaHangHoa_ThueChild ThueChild = muaHangHoaViewModel.Model.MSP_MuaHangHoa_Thues[e.Index];
            IList<DM_ThueSuatInfo> list = dm_ThueSuat_ListViewModel.Model.getListOfInfo();
            foreach (DM_ThueSuatInfo item in list)
            {
                if (item.Khoa == e.ThueSuatId)
                {
                    ThueChild.ThueSuatValue = item.GiaTriTinhThue;
                    break;
                }
            }
        }

        void msp_MuaDichVu_ThueSuatIdChanged(object sender, ThueSuatIdChangedEventArgs e)
        {
            System.Windows.Data.CollectionViewSource msp_MuaDichVu_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaDichVu_ViewModelSource"];
            MSP_MuaDichVu_ViewModel muaDichVuViewModel = ((List<object>)msp_MuaDichVu_CollectionViewSource.Source)[0] as MSP_MuaDichVu_ViewModel;

            System.Windows.Data.CollectionViewSource dm_ThueSuat_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_ThueSuat_ListViewModelSource"];
            DM_ThueSuat_ListViewModel dm_ThueSuat_ListViewModel = ((List<object>)dm_ThueSuat_CollectionViewSource.Source)[0] as DM_ThueSuat_ListViewModel;

            MSP_MuaDichVu_ThueChild ThueChild = muaDichVuViewModel.Model.MSP_MuaDichVu_Thues[e.Index];
            IList<DM_ThueSuatInfo> list = dm_ThueSuat_ListViewModel.Model.getListOfInfo();
            foreach (DM_ThueSuatInfo item in list)
            {
                if (item.Khoa == e.ThueSuatId)
                {
                    ThueChild.ThueSuatValue = item.GiaTriTinhThue;
                    break;
                }
            }


        }
        void msp_MuaDichVu_HangHoaIdChanged(object sender, HangHoaIdChangedEventArgs e)
        {
        }
        void msp_MuaHangHoa_HangHoaIdChanged(object sender, HangHoaIdChangedEventArgs e)
        {
            System.Windows.Data.CollectionViewSource msp_MuaHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaHangHoa_ViewModelSource"];
            MSP_MuaHangHoa_ViewModel muaHangHoaViewModel = ((List<object>)msp_MuaHangHoa_CollectionViewSource.Source)[0] as MSP_MuaHangHoa_ViewModel;

            MSP_MuaHangHoa_ChiTietChild ChiTietChild = muaHangHoaViewModel.Model.MSP_MuaHangHoa_ChiTiets[e.Index];
            MSP_MuaHangHoa_ThueChild ThueChild = muaHangHoaViewModel.Model.MSP_MuaHangHoa_Thues[e.Index];
            MSP_MuaHangHoa_ChiPhiMuaHangChild ChiPhiMuaHangChild = muaHangHoaViewModel.Model.MSP_MuaHangHoa_ChiPhiMuaHangs[e.Index];
            MSP_MuaHangHoa_TSThuaCXLChild TSThuaCXLChild = muaHangHoaViewModel.Model.MSP_MuaHangHoa_TSThuaCXLs[e.Index];
            MSP_MuaHangHoa_TSThieuCXLChild TSThieuCXLChild = muaHangHoaViewModel.Model.MSP_MuaHangHoa_TSThieuCXLs[e.Index];
            MSP_MuaHangHoa_ThueNKChild ThueNKChild = muaHangHoaViewModel.Model.MSP_MuaHangHoa_ThueNKs[e.Index];
            MSP_MuaHangHoa_ThueTTDBChild ThueTTDBChild = muaHangHoaViewModel.Model.MSP_MuaHangHoa_ThueTTDBs[e.Index];

            System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
            DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = ((List<object>)dm_HangHoa_CollectionViewSource.Source)[0] as DM_HangHoa_ListViewModel;
            System.Collections.Generic.IList<DM_HangHoaInfo> list = dm_HangHoa_ListViewModel.Model.getListOfInfo();

            System.Windows.Data.CollectionViewSource dm_TaiKhoan_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoan_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TaiKhoan_ListViewModel = ((List<object>)dm_TaiKhoan_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            System.Collections.Generic.IList<DM_TaiKhoanInfo> listTaiKhoan = dm_TaiKhoan_ListViewModel.Model.getListOfInfo();


            foreach (DM_HangHoaInfo item in list)
            {
                if (item.Khoa == e.HangHoaId)
                {
                    ChiTietChild.DienGiai = item.TenViet;
                    ChiTietChild.KhoId = item.KhoNgamDinhId;
                    ChiTietChild.TKKhoId = (int)item.TKKhoId;
                    ChiTietChild.DonGia = item.DonGiaBan;
                    ChiTietChild.TKNoId = (int)item.TKKhoId;
                    ChiTietChild.TKCoId = listTaiKhoan[0].Khoa;
                    ChiTietChild.DVTId = item.DonViTinhId;

                    ThueChild.HangHoaId = item.Khoa;
                    ThueChild.DienGiai = item.TenViet;
                    ThueChild.LoaiHoaDonId = muaHangHoaViewModel.Model.LoaiHoaDonId;
                    ThueChild.ThueSuatId = item.ThueSuatId;
                    ThueChild.TKNoId = listTaiKhoan[0].Khoa;
                    ThueChild.TKCoId = ChiTietChild.TKCoId;


                    ThueNKChild.HangHoaId = item.Khoa;
                    ThueNKChild.DienGiai = item.TenViet;
                    ThueNKChild.TkNoId = listTaiKhoan[0].Khoa;
                    ThueNKChild.TkCoId = listTaiKhoan[0].Khoa;



                    TSThuaCXLChild.HangHoaId = item.Khoa;
                    TSThuaCXLChild.DienGiai = item.TenViet;
                    TSThuaCXLChild.TkNoId = listTaiKhoan[0].Khoa;
                    TSThuaCXLChild.TkCoId = listTaiKhoan[0].Khoa;

                    TSThieuCXLChild.HangHoaId = item.Khoa;
                    TSThieuCXLChild.DienGiai = item.TenViet;
                    TSThieuCXLChild.TkNoId = listTaiKhoan[0].Khoa;
                    TSThieuCXLChild.TkCoId = listTaiKhoan[0].Khoa;


                    ChiPhiMuaHangChild.HangHoaId = item.Khoa;
                    ChiPhiMuaHangChild.DienGiai = item.TenViet;
                    ChiPhiMuaHangChild.TkNoId = listTaiKhoan[0].Khoa;
                    ChiPhiMuaHangChild.TkCoId = listTaiKhoan[0].Khoa;

                    ThueTTDBChild.HangHoaId = item.Khoa;
                    ThueTTDBChild.DienGiai = item.TenViet;
                    ThueTTDBChild.TkNoId = listTaiKhoan[0].Khoa;
                    ThueTTDBChild.TkCoId = listTaiKhoan[0].Khoa;

                    break;
                }
            }
        }
        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
           if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
            {

                System.Windows.Data.CollectionViewSource msp_MuaHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaHangHoa_ViewModelSource"];
                MSP_MuaHangHoa_ViewModel muaHangHoaViewModel = ((List<object>)msp_MuaHangHoa_CollectionViewSource.Source)[0] as MSP_MuaHangHoa_ViewModel;

                if (this.GetViewModel().CanSave)
                {
                    this.GetViewModel().SavePhieuChi();
                }
                if (muaHangHoaViewModel.CanSave)
                {
                    muaHangHoaViewModel.SaveMuaHangHoa();
                }

            }
            else if (this.MuaDichVuTabControl.Visibility == Visibility.Visible)
            {

                System.Windows.Data.CollectionViewSource msp_MuaDichVu_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaDichVu_ViewModelSource"];
                MSP_MuaDichVu_ViewModel muaDichVuViewModel = ((List<object>)msp_MuaDichVu_CollectionViewSource.Source)[0] as MSP_MuaDichVu_ViewModel;

                if (this.GetViewModel().CanSave)
                {
                    this.GetViewModel().SavePhieuChi();
                }
                if (muaDichVuViewModel.CanSave)
                {
                    muaDichVuViewModel.SaveMuaDichVu();
                }

            }
        }


       

        private void AddMuaHangHoaDetailsItem()
        {
            GetMuaHangHoaViewModel().AddMSP_MuaHangHoa_ChiTietItem();
        }


        private void DeleteMuaHangHoaDetailsItem(int Index)
        {
            GetMuaHangHoaViewModel().DeleteMSP_MuaHangHoa_ChiTietItem(Index);
        }

        private void AddMuaDichVuDetailsItem()
        {
            GetMuaDichVuViewModel().AddMSP_MuaDichVu_ChiTietItem();
        }

        private void DeleteMuaDichVuDetailsItem(int Index)
        {
            GetMuaDichVuViewModel().DeleteMSP_MuaDichVu_ChiTietItem(Index);
        }


        void msp_MuaHangHoa_ViewModel_ChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (e.ChildListName == "MSP_MuaHangHoa_ChiTiets")
            {
                this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "MSP_MuaHangHoa_Thues")
            {
                this.MSP_MuaHangHoa_ChiPhiMuaHangGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "MSP_MuaHangHoa_ChiPhiMuaHangs")
            {
                this.MSP_MuaHangHoa_ChiPhiMuaHangGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "MSP_MuaHangHoa_TSThieuCXLs")
            {
                this.MSP_MuaHangHoa_TSThieuCXLGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "MSP_MuaHangHoa_TSThuaCXLs")
            {
                this.MSP_MuaHangHoa_TSThuaCXLGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "MSP_MuaHangHoa_ThueNKs")
            {
                this.MSP_MuaHangHoa_ThueNKGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "MSP_MuaHangHoa_ThueTTDBs")
            {
                this.MSP_MuaHangHoa_ThueTTDBGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
        }

        void MSP_MuaDichVu_ViewModel_ChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (e.ChildListName == "MSP_MuaDichVu_ChiTiets")
            {
                this.MSP_MuaDichVu_MuaDichVuChiTietGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "MSP_MuaDichVu_Thues")
            {
                this.MSP_MuaDichVu_ChiPhiMuaDichVuGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "MSP_MuaDichVu_ChiPhiMuaDichVus")
            {
                this.MSP_MuaDichVu_ChiPhiMuaDichVuGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }

        }
    }
}


